<template>
  <div class="page-live-liveUser">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"

        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="name"
            :label="$t('liveRoom.roomName')+':'"
          >
            <el-input
              v-model="searchForm.name"
              type="text"
              clearable
              :placeholder="$t('liveRoom.roomName')"
            />
          </el-form-item>
          <el-form-item
            prop="nickName"
            :label="$t('liveRoom.anchorNickname')+':'"
          >
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('liveRoom.anchorNickname')"
            />
          </el-form-item>
          <el-form-item
            prop="liveStatus"
            :label="$t('liveRoom.liveRoomStatus')+':'"
          >
            <el-select
              v-model="searchForm.liveStatus"
              clearable
              :placeholder="$t('liveRoom.liveRoomStatus')"
            >
              <el-option
                v-for="item in liveStatus"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('liveRoom.liveBroadcastTime')+':'">
            <el-date-picker
              v-model="dateRange"
              clearable
              type="datetimerange"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0),
                              new Date(2000, 2, 1, 23, 59, 59)]"
              range-separator="至"
              value-format="YYYY-MM-DD HH:mm:ss"
              :start-placeholder="$t('date.start')"
              :end-placeholder="$t('date.end')"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="resetForm"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <!-- 搜索栏end -->
    <div class="main-container">
      <div class="table-con">
        <el-table
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <el-table-column
            :label="$t('brand.serialNumber')"
            type="index"
            width="80"
          />
          <el-table-column
            prop="name"
            :label="$t('liveRoom.roomName')"
            width="200"
          >
            <template #default="scope">
              <span class="table-cell-text">
                {{ scope.row.name }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            prop="nickName"
            :label="$t('liveRoom.anchorNickname')"
            width="200"
          >
            <template #default="scope">
              <span class="table-cell-text">
                {{ scope.row.nickName }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            prop="userMobile"
            :label="$t('liveRoom.anchorMobile')"
          />
          <el-table-column
            prop="liveStatus"
            :label="$t('liveRoom.liveRoomStatus')"
          >
            <template #default="scope">
              <span
                v-if="scope.row.liveStatus === 1"
              >{{ $t('liveRoom.live') }}</span>
              <span v-if="scope.row.liveStatus === -1">{{ $t('liveRoom.offline') }}</span>
              <span v-if="scope.row.liveStatus === 0">{{ $t('liveRoom.noStart') }}</span>
              <span v-if="scope.row.liveStatus === 2">{{ $t('liveRoom.finished') }}</span>
              <span v-if="scope.row.liveStatus === 3">{{ $t('liveRoom.stop') }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="startTime"
            :label="$t('liveRoom.LiveStartTime')"
            width="160"
          />
          <el-table-column
            prop="endTime"
            :label="$t('liveRoom.LiveOverTime')"
            width="160"
          />
          <el-table-column
            :label="$t('crud.menu')"
            fixed="right"
            align="center"
            width="250"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row)"
                >
                  {{ $t("liveRoom.view") }}
                </div>
                <div
                  v-if="isAuth('platform:liveRoom:offline') && scope.row.liveStatus !== -1 && scope.row.liveStatus !== 2"
                  class="default-btn text-btn"
                  @click="offlineLiveHandle(scope.row)"
                >
                  {{ $t("coupon.violationOffShelf") }}
                </div>
                <div
                  v-if="scope.row.liveStatus === -1"
                  class="default-btn text-btn"
                  @click="getOfflineRemark(scope.row.remark)"
                >
                  {{ $t("groups.reasonForOffline") }}
                </div>
                <div
                  v-if="isAuth('platform:liveRoom:delete') && scope.row.liveStatus !== 1"
                  class="default-btn text-btn"
                  type="text"
                  @click="onDelete(scope.row.roomId)"
                >
                  {{ $t('user.deleteBtn') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>
  </div>
  <add-or-update
    v-if="addOrUpdateVisible"
    ref="addOrUpdateRef"
    @refresh-data-list="refreshChange"
  />
</template>

<script setup>
import { isAuth } from '@/utils'
import { ElMessage, ElMessageBox } from 'element-plus'
import AddOrUpdate from './add-or-update.vue'
const liveStatus = [
  {
    value: -1,
    label: $t('liveRoom.offline')
  }, {
    value: 0,
    label: $t('liveRoom.noStart')
  }, {
    value: 1,
    label: $t('liveRoom.live')
  }, {
    value: 2,
    label: $t('liveRoom.finished')
  }, {
    value: 3,
    label: $t('liveRoom.stop')
  }
]

onMounted(() => {
  getDataList(page)
})

let theparams = null
let tempSearchForm = null // 保存上次点击查询的请求条件
const dateRange = ref([]) // 时间区间
const dataListLoading = ref(false)
const dataList = ref([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  name: '',
  nickName: '',
  liveStatus: '',
  roomTop: ''
}) // 搜索
const getDataList = (pageParam, newData = false) => {
  dataListLoading.value = true
  if (page) {
    const size = Math.ceil(page.total / page.pageSize)
    page.currentPage = (page.currentPage > size ? size : page.currentPage) || 1
  }
  if (newData || !tempSearchForm) {
    theparams = JSON.parse(JSON.stringify(searchForm))
    tempSearchForm = {
      current: pageParam == null ? page.currentPage : pageParam.currentPage,
      size: pageParam == null ? page.pageSize : pageParam.pageSize,
      startTime: dateRange.value === null ? null : dateRange.value[0], // 开始时间
      endTime: dateRange.value === null ? null : dateRange.value[1] // 结束时间
    }
  } else {
    tempSearchForm.current = pageParam == null ? page.currentPage : pageParam.currentPage
    tempSearchForm.size = pageParam == null ? page.pageSize : pageParam.pageSize
  }
  http({
    url: http.adornUrl('/live/liveRoom/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(tempSearchForm, theparams)
    )
  })
    .then(({ data }) => {
      dataList.value = data.records
      page.total = data.total
      dataListLoading.value = false
    })
}
const onDelete = (id) => {
  ElMessageBox.confirm($t('seckill.isDeleOper'), $t('remindPop.remind'), {
    confirmButtonText: $t('remindPop.confirm'),
    cancelButtonText: $t('remindPop.cancel'),
    type: 'warning'
  })
    .then(() => {
      http({
        url: http.adornUrl('/live/liveRoom/' + id),
        method: 'delete',
        data: http.adornData({})
      })
        .then(() => {
          page.total = page.total - 1
          ElMessage({
            message: $t('remindPop.success'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              refreshChange()
            }
          })
        })
    })
}

/**
 * 刷新回调
 */
const refreshChange = () => {
  getDataList(page)
}
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}
const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}
const searchFormRef = ref(null)
const resetForm = () => {
  searchFormRef.value.resetFields() // 清空筛选列表
  dateRange.value = []
}
/**
 * 下线活动
 * @param row
 */
const offlineLiveHandle = (row) => {
  let tips = $t('seckill.offlineTip')
  tips = tips.replace('ACTIVITYNAME', row.name)
  ElMessageBox.prompt(tips, $t('text.tips'), {
    confirmButtonText: $t('coupon.confirm'),
    cancelButtonText: $t('coupon.cancel'),
    inputPattern: /\s\S+|S+\s|\S/, // 不能全为空格 空字符串
    inputErrorMessage: $t('seckill.offlineReasonTip'),
    inputValidator: (value) => {
      if (value.length > 100) {
        return $t('seckill.offlineReasonTip1')
      }
    }
  })
    .then((obj) => {
      http({
        url: http.adornUrl('/live/liveRoom/offline'),
        method: 'post',
        data: http.adornData({
          roomId: row.roomId,
          remark: obj.value || ''
        })
      })
        .then(() => {
          ElMessage({
            message: $t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              getDataList(page)
            }
          })
        })
    })
}
/**
 * 查看下线原因
 * @param remark
 */
const getOfflineRemark = (remark) => {
  ElMessageBox.confirm(remark, $t('groups.reasonForOffline'), {
    confirmButtonText: $t('remindPop.confirm')
  })
}
const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)
/**
 * 新增 / 修改
 * @param id
 */
const onAddOrUpdate = (data) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(JSON.parse(JSON.stringify(data)))
  })
}
</script>

<style lang="scss" scoped>
.page-live-liveUser {
  :deep(.tips .text) {
    color: #FF0000;
  }
}
:deep(.el-message-box__message) p{
  line-break: anywhere;
}
</style>
